package com.siwei.mes.controller.experiment;

import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.experiment.ExperimentGenConfig;
import com.siwei.mes.service.experiment.ExperimentGenConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账生成委托规则设置相关
 *
 * @author linzi
 * @since 2024-05-22 21:06:06
 */
@Api(value = "试验台账生成委托规则设置")
@RestController
@RequestMapping("experimentGenConfig")
public class ExperimentGenConfigController {
    @Resource
    private ExperimentGenConfigService experimentGenConfigService;

    @ApiOperation(value = "修改试验台账生成委托规则设置")
    @PostMapping("/updateList")
    public Json update(@RequestBody @Validated(UpdateGroup.class) ExperimentGenConfig entity) {
        List<ExperimentGenConfig> configList = entity.getConfigList();
        assertTrue(configList == null || configList.isEmpty(), "委托规则设置为空");
        for (ExperimentGenConfig genConfig : configList) {
            experimentGenConfigService.update(genConfig);
        }
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "获取所有试验台账生成委托规则设置")
    @PostMapping("getAll")
    public Json getAll(@RequestBody ExperimentGenConfig entity) {
        return Json.succ().data(ApiResult.addList(experimentGenConfigService.getAll(entity)));
    }

}
